#-*-coding=utf-8-*-
from django.shortcuts import *
from django.http import *
from django.template import *
from google.appengine.ext import db
from auth.decorators import *
from models import *
from forms import *
from django.core.urlresolvers import reverse
from utils.url import *
from urlflow.models import Url

@login_required
def save(request, template, key = None):
    '''
功能：目录及链接的增加，修改操作。
    '''
    if request.method == "POST":
        title = clean(request.POST.get("title", ""))
        url = complete_url(clean(request.POST.get("url", "")))
        pk = clean(request.POST.get("parent", ""))
        is_dir = clean(request.POST.get("is_dir", ""))
        if key == "" or key == None:
            if is_dir == "":
                FavorSite(user = request.user, title = title, url = url, paren_key = pk).put()
            else:
                FavorSite(user = request.user, title = title, paren_key = pk, is_dir = "true").put()
        else:
            site = FavorSite.get(key)
            site.title = title
            site.url = complete_url(url)
            site.paren_key = pk
            site.put()
        return HttpResponseRedirect(clean(request.POST.get("next")))
    else:
        pk = clean(request.GET.get("parent", ""))
        if key == "" or key == None:
            url = clean(request.GET.get("url", ""))
            site = FavorSite(user = request.user, title = url, url = url)
        else:
            site = FavorSite.get(key)
        if site.is_dir == "true":
            dirs = FavorSite.gql("where user=:1 and is_dir='true' and __key__!=:2", request.user, site.key())
        else:
            dirs = FavorSite.gql("where user=:1 and is_dir='true'", request.user)
        if pk != "":
            pk = db.Key(pk)
        return render_to_response(template, {"obj":site,
                                             "dirs":dirs,
                                             "parent":pk,
                                             "is_dir":clean(request.GET.get("is_dir", "")),
                                             "next":clean(request.GET.get("next", ""))})
    

@login_required
def index(request, template):
    '''
功能：返回用户全部的目录及链接信息
    '''
    objs = FavorSite.gql("where user=:1 order by weight desc", request.user)
    return render_to_response(template, {"objs":objs}, context_instance=RequestContext(request))
    

def query_children(objs, pk):
    '''
功能：查询子节点。
    '''
    query = []
    for obj in objs:
        if obj.paren_key == pk:
            query.append(obj)
    return query

    
def delete_obj(objs, key):
    '''
功能：级联删除目录下的链接。
    '''
    children = query_children(objs, key)
    for child in children:
        delete_obj(objs, str(child.key()))
    for i in objs:
        if str(i.key()) == key:
            i.delete()

    
@login_required
def delete(request, key):
    '''
功能：删除链接和目录，能够级联删除目录下的链接。
    '''
    objs = FavorSite.gql("where user=:1", request.user)
    delete_obj(objs, key)
    return HttpResponseRedirect(request.GET.get("next",reverse("favorite.views.index")))


@login_required
def redirect(request, key):
    '''
功能：记录链接的使用次数，用来做排序。
    '''
    redirect = FavorSite.get(key)
    Url.scroll_input(redirect.title, redirect.url)
    while key:
        site = FavorSite.get(key)
        site.weight += 1
        site.put()
        key = site.paren_key
    return HttpResponseRedirect(redirect.url)


